<template>
  <div>
    <mytop :title="title" />
    <div class="warp">
      <van-address-edit
        :area-list="areaList"
        show-postal
        show-set-default
         show-delete
        :address-info="initInfo"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
      />
    </div>
  </div>
</template>

<script>
import mytop from 'components/My-Top'
import areaList from 'utils/area'
import { mapMutations, mapState } from 'vuex'
import { Toast } from 'vant'
export default {
  data () {
    return {
      title: '修改地址',
      areaList,
      initInfo: {}
    }
  },
  created () {
    this.initAddressInfo()
  },
  methods: {
    onDelete (content) {
      console.log(content)
      this.delet_Address(parseInt(this.$route.query.index))
      Toast('删除成功')
      this.$router.go(-1)
    },
    initAddressInfo () {
      this.initInfo = this.address[parseInt(this.$route.query.index)]
      // 在地址栏上面 也就是也i按跳转的网址上面会自动更新成字符串 所以需要parseInt一下
    },
    ...mapMutations('address', ['add_adress', 'update_Address', 'delet_Address']),
    onSave (content) {
      this.update_Address({
        content,
        index: parseInt(this.$route.query.index)
      })
      Toast('修改成功')
      this.$router.go(-1)
    }
  },
  computed: {
    ...mapState({
      address: state => state.address.address
    })
  },
  components: {
    mytop
  }
}
</script>

<style lang="scss" scoped>
.warp {
  margin-top: 46px;
  margin-bottom: 50px;
}
</style>
